package com.ruoyi.controller.jpjs.municipal;

import com.ruoyi.common.constant.MsgConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.jpjs.common.municipal.AnnualTarget;
import com.ruoyi.domain.jpjs.common.municipal.IncomeDetail;
import com.ruoyi.domain.jpjs.common.municipal.IncomeParam;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.jpjs.municipal.TargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author C.fw
 * @explain 危大工程控制层
 * @since 2020/8/15 15:43
 */
@Controller
@RequestMapping("/target")
public class TargetController extends BaseController {

    private String prefix = "jpjs/municipal";

    @Autowired
    private TargetService targetService;

    /**
     * 首页面
     */
    @GetMapping()
    public String view(ModelMap mmp) {
        return prefix + "/list";
    }

    /**
     * 首页面 -> 初始化数据
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AnnualTarget annualTarget) {
        startPage();
        annualTarget.setProgramName(annualTarget.getProgramName().trim().replaceAll(",", ""));
        // 获取当前登录人对应的项目权限
        List<AnnualTarget> annualTargets = targetService.queryTargetList(annualTarget);
        return getDataTable(annualTargets);
    }

    /**
     * 首页面 -> 批量删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(targetService.deleteTargetById(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 新增页面
     */
    @GetMapping("/addView")
    public String addView(ModelMap mmp, String programName, String companyId, String programId) {
        Map<String, Object> map = new HashMap<>();
        mmp.put("companyId", companyId);
        mmp.put("programId", programId);
        mmp.put("programName", programName);
        return prefix + "/add";
    }

    /**
     * 新增页面 -> 保存
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(AnnualTarget annualTarget) {
        // Step1 校验 同一个项目下的年份的唯一性
        if (MsgConstants.MONTH_NOT_UNIQUE.equals(targetService.checkActualYearUnique(annualTarget))) {
            return error("新增目标,存在重复年份!");
        }

        // Step2 新增产值表
        return toAjax(targetService.addTarget(annualTarget));
    }

    /**
     * 更新页面
     */
    @GetMapping("/editView")
    public String editView(ModelMap mmp, String targetId) {
        Map<String, Object> map = new HashMap<>();
        // 产值信息
        AnnualTarget annualTarget = new AnnualTarget();
        annualTarget.setTargetId(Integer.valueOf(targetId));
        List<AnnualTarget> annualTargets = targetService.queryTargetList(annualTarget);
        mmp.put("annualTarget", annualTargets.get(0));
        return prefix + "/edit";
    }

    /**
     * 更新页面 -> 编辑
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(AnnualTarget annualTarget) {
        // Step1 校验 计划月份、实际月份唯一性
        if (MsgConstants.MONTH_NOT_UNIQUE.equals(targetService.checkActualYearUniqueEdit(annualTarget))) {
            return error("更新目标,存在重复年份!");
        }
        return toAjax(targetService.updateTarget(annualTarget));
    }


}
